/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.wadl.fedora34;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;

import javax.activation.DataSource;
import javax.xml.bind.JAXBContext;
//import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import org.apache.commons.mail.ByteArrayDataSource;
import org.apache.log4j.Logger;
import org.igfay.jfig.JFig;
import org.igfay.jfig.JFigException;
import org.jvnet.ws.wadl.util.DSDispatcher;
//import org.jvnet.ws.wadl.util.JAXBDispatcher;
import org.jvnet.ws.wadl.util.UriBuilder;

import uk.ljmu.fedoraifz.restrequests.UserCredentials;
import uk.ljmu.fedoraifz.utilities.GeneralUtilities;

public class Pid {
    private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
    //private JAXBDispatcher _jaxbDispatcher;
    private DSDispatcher _dsDispatcher;
    private UriBuilder _uriBuilder;
    @SuppressWarnings("unused")
    private JAXBContext _jc;
    private HashMap<String, Object> _templateAndMatrixParameterValues;
    private List<String> matrixParamSet;
    private DataSource retVal;
    private static Logger log = Logger.getLogger(Pid.class);

    /**
     * Create new instance
     * 
     */
    public Pid(String pid)
    throws JAXBException
    {
	_dsDispatcher = new DSDispatcher();
	_uriBuilder = new UriBuilder();
	String basePath;
	try {
	    basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	} catch (JFigException e) {
	    log.error("Error: ", e);
	    //Error, use default Fedora path
	    basePath = "http://localhost:8080/fedora";
	}
	matrixParamSet = _uriBuilder.addPathSegment(basePath);
	matrixParamSet = _uriBuilder.addPathSegment("/objects");
	matrixParamSet = _uriBuilder.addPathSegment("/{pid}");
	_templateAndMatrixParameterValues = new HashMap<String, Object>();
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    /**
     * Get pid
     * 
     */
    public String getPid() {
	return ((String) _templateAndMatrixParameterValues.get("pid"));
    }

    public void setMatrixParamSet(List<String> matrixParamSet) {
	this.matrixParamSet = matrixParamSet;
    }

    public List<String> getMatrixParamSet() {
	return matrixParamSet;
    }

    public void setRetVal(DataSource retVal) {
	this.retVal = retVal;
    }

    public DataSource getRetVal() {
	return retVal;
    }

    /**
     * Set pid
     * 
     */
    public void setPid(String pid) {
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    public void post()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	setRetVal(_dsDispatcher.doPOST(new ByteArrayDataSource("", DEFAULT_CONTENT_TYPE),
		DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null));
	return ;
    }

    public void post(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	setRetVal(_dsDispatcher.doPOST(new ByteArrayDataSource("", DEFAULT_CONTENT_TYPE),
		DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null));
	return ;
    }
    public void post(UserCredentials userCreds, String mimetype)
    throws IOException, MalformedURLException {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	// AMG authorization
	_headerParameterValues.put("Authorization", userCreds
		.getEncodedCredentials());

	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues,
		_queryParameterValues);
	setRetVal(_dsDispatcher.doPOST(new ByteArrayDataSource("", mimetype),
		mimetype, _url, _headerParameterValues, null));
	return;
    }
    /* public void postAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/
    public void post(UserCredentials userCreds, String contents,
		String mimetype, String label, String logmessage, String format,
		String encoding, String namespace, String ownerid, String state,
		Boolean ignoremime) throws IOException, MalformedURLException {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	// AMG authorization
	_headerParameterValues.put("Authorization", userCreds
			.getEncodedCredentials());
	//AMG- check parameters
	if (label != null && !label.equals(""))
		_queryParameterValues.put("label", label);
	if (logmessage != null && !logmessage.equals(""))
		_queryParameterValues.put("logMessage", logmessage);
	if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	if (encoding != null && !encoding.equals(""))
		_queryParameterValues.put("encoding", encoding);
	if (namespace != null && !namespace.equals(""))
		_queryParameterValues.put("namespace", namespace);
	if (ownerid != null && !ownerid.equals(""))
		_queryParameterValues.put("ownerId", ownerid);
	if (state != null && !state.equals(""))
		_queryParameterValues.put("state", state);
	if (ignoremime != null && !ignoremime.equals(""))
		_queryParameterValues.put("ignoreMime", ignoremime);
	
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues,
			_queryParameterValues);
	// AMG - Check if contents come from URL or String
	if (contents.startsWith(GeneralUtilities.URL_PROTOCOL)) {
		String newContents = GeneralUtilities.getURLcontents(contents);
		retVal = _dsDispatcher.doPOST(new ByteArrayDataSource(newContents,
				mimetype), mimetype, _url, _headerParameterValues, null);
	} else {
		retVal = _dsDispatcher.doPOST(new ByteArrayDataSource(contents,
				mimetype), mimetype, _url, _headerParameterValues, null);
	}
}
    public void post(String contents, String mimetype, String label, String logmessage, String format, String encoding, String namespace, String ownerid, String state, Boolean ignoremime)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (label != null && !label.equals(""))
	    _queryParameterValues.put("label", label);
	if (logmessage != null && !logmessage.equals(""))
	    _queryParameterValues.put("logMessage", logmessage);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);
	if (encoding != null && !encoding.equals(""))
	    _queryParameterValues.put("encoding", encoding);
	if (namespace != null && !namespace.equals(""))
	    _queryParameterValues.put("namespace", namespace);
	if (ownerid != null && !ownerid.equals(""))
	    _queryParameterValues.put("ownerId", ownerid);
	if (state != null && !state.equals(""))
	    _queryParameterValues.put("state", state);
	if (ignoremime != null && !ignoremime.equals(""))
	    _queryParameterValues.put("ignoreMime", ignoremime);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doPOST(new ByteArrayDataSource(contents,
		mimetype), mimetype, _url, _headerParameterValues, null);
	return ;
    }

    /* public void postAsvoid(String label, String logmessage, String format, String encoding, String namespace, String ownerid, String state, Boolean ignoremime)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("label", label);
	_queryParameterValues.put("logMessage", logmessage);
	_queryParameterValues.put("format", format);
	_queryParameterValues.put("encoding", encoding);
	_queryParameterValues.put("namespace", namespace);
	_queryParameterValues.put("ownerId", ownerid);
	_queryParameterValues.put("state", state);
	_queryParameterValues.put("ignoreMime", ignoremime);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void get()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*  public void getAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void get(String asofdatetime, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (asofdatetime != null && !asofdatetime.equals(""))
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds, String asofdatetime, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	if (asofdatetime != null && !asofdatetime.equals(""))
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*public void getAsvoid(String asofdatetime, String format)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("asOfDateTime", asofdatetime);
	_queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void delete()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    public void delete(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    /*public void deleteAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doDELETE(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void delete(String logmessage)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (logmessage != null && !logmessage.equals(""))
	    _queryParameterValues.put("logMessage", logmessage);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    public void delete(UserCredentials userCreds, String logmessage)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	if (logmessage != null && !logmessage.equals(""))
	    _queryParameterValues.put("logMessage", logmessage);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    /*public void deleteAsvoid(String logmessage)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("logMessage", logmessage);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doDELETE(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void put(String contents, String mimetype)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doPUT(new ByteArrayDataSource(contents,
		mimetype), mimetype, _url, _headerParameterValues, null);
	return ;
    }

    public void put(UserCredentials userCreds, String contents, String mimetype)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doPUT(new ByteArrayDataSource(contents,
		mimetype), mimetype, _url, _headerParameterValues, null);
	return ;
    }

    /* public void putAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doPUT(null, null, _url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void put(String label, String logmessage, String ownerid, String state, String lastmodifieddate, String contents, String mimetype)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (label != null && !label.equals(""))
	    _queryParameterValues.put("label", label);
	if (logmessage != null && !logmessage.equals(""))
	    _queryParameterValues.put("logMessage", logmessage);
	if (ownerid != null && !ownerid.equals(""))
	    _queryParameterValues.put("ownerId", ownerid);
	if (state != null && !state.equals(""))
	    _queryParameterValues.put("state", state);
	if (lastmodifieddate != null && !lastmodifieddate.equals(""))
	    _queryParameterValues.put("lastModifiedDate", lastmodifieddate);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doPUT(new ByteArrayDataSource(contents,
		mimetype), mimetype, _url, _headerParameterValues, null);
	return ;
    }

    public void put(UserCredentials userCreds, String label, String logmessage, String ownerid, String state, String lastmodifieddate, String contents, String mimetype)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	if (label != null && !label.equals(""))
	    _queryParameterValues.put("label", label);
	if (logmessage != null && !logmessage.equals(""))
	    _queryParameterValues.put("logMessage", logmessage);
	if (ownerid != null && !ownerid.equals(""))
	    _queryParameterValues.put("ownerId", ownerid);
	if (state != null && !state.equals(""))
	    _queryParameterValues.put("state", state);
	if (lastmodifieddate != null && !lastmodifieddate.equals(""))
	    _queryParameterValues.put("lastModifiedDate", lastmodifieddate);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doPUT(new ByteArrayDataSource(contents,
		mimetype), mimetype, _url, _headerParameterValues, null);
	return ;
    }

    /*public void putAsvoid(String label, String logmessage, String ownerid, String state, String lastmodifieddate)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("label", label);
	_queryParameterValues.put("logMessage", logmessage);
	_queryParameterValues.put("ownerId", ownerid);
	_queryParameterValues.put("state", state);
	_queryParameterValues.put("lastModifiedDate", lastmodifieddate);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doPUT(null, null, _url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public static class Export {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	/**
	 * Create new instance
	 * 
	 */
	public Export(String pid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}");
	    matrixParamSet = _uriBuilder.addPathSegment("/export");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String format, String context, String encoding)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    if (context != null && !context.equals(""))
		_queryParameterValues.put("context", context);
	    if (encoding != null && !encoding.equals(""))
		_queryParameterValues.put("encoding", encoding);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String format, String context, String encoding)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    if (context != null && !context.equals(""))
		_queryParameterValues.put("context", context);
	    if (encoding != null && !encoding.equals(""))
		_queryParameterValues.put("encoding", encoding);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String format, String context, String encoding)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("format", format);
	    _queryParameterValues.put("context", context);
	    _queryParameterValues.put("encoding", encoding);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

    public static class ObjectXML {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public ObjectXML(String pid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    setMatrixParamSet(_uriBuilder.addPathSegment("/objects"));
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}");
	    matrixParamSet = _uriBuilder.addPathSegment("/objectXML");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    // AMG authorization
	    _headerParameterValues.put("Authorization", userCreds
		    .getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

    public static class Validate {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public Validate(String pid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}");
	    matrixParamSet = _uriBuilder.addPathSegment("/validate");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(UserCredentials userCreds, String asofdatetime)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();

	    //AMG - Authorization
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(String asofdatetime)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String asofdatetime)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

    public static class Versions {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public Versions(String pid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}");
	    matrixParamSet = _uriBuilder.addPathSegment("/versions");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String format)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }
}